﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;


/// <summary>
/// Summary description for Advertise_session
/// </summary>
public class Advertise_session
{
    private static SqlConnection sqlConnection;

    private DataTable objDataTable;
    public static SqlDataAdapter sqlAdapter;
    public static SqlCommand sqlCommand; 
	public Advertise_session()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static List<Advertise> GetList_Advertise()
    {
        DataTable dt = DataProvider.GetDataTable("select * from Advertise");
        List<Advertise> lst = new List<Advertise>();

        int  i =0;
        for (i = 0; i < dt.Rows.Count; i++)
        {
            Advertise obj = new Advertise();

            obj.Adv_ID = (dt.Rows[i]["Adv_ID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[i]["Adv_ID"];
            obj.Adv_Title = (dt.Rows[i]["Adv_Title"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["Adv_Title"];
            obj.Adv_Link = (dt.Rows[i]["Adv_Link"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["Adv_Link"];
            obj.Adv_Status = (dt.Rows[i]["Adv_Status"] is DBNull) ? false : (System.Boolean)dt.Rows[i]["Adv_Status"];
            obj.Adv_Position = (dt.Rows[i]["Adv_Position"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[i]["Adv_Position"];
            obj.Adv_Width = (dt.Rows[i]["Adv_Width"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[i]["Adv_Width"];
            obj.Adv_Height = (dt.Rows[i]["Adv_Height"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[i]["Adv_Height"];
            obj.Adv_CreateDate = (dt.Rows[i]["Adv_CreateDate"] is DBNull) ? DateTime.MinValue : (System.DateTime)dt.Rows[i]["Adv_CreateDate"];
            obj.Adv_EndDate = (dt.Rows[i]["Adv_EndDate"] is DBNull) ? DateTime.MinValue : (System.DateTime)dt.Rows[i]["Adv_EndDate"];
            obj.Adv_Desc = (dt.Rows[i]["Adv_Desc"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["Adv_Desc"];
            obj.Adv_Image = (dt.Rows[i]["Adv_Image"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["Adv_Image"];
            obj.Cate_ID = (dt.Rows[i]["Cate_ID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[i]["Cate_ID"];

            lst.Add(obj);
        }

        //WebMsgBox.Show(i.ToString());
        return lst;
    }
    public static Advertise Get_Advertise_ByID(System.Int32 Adv_ID)
    {
        Advertise obj = new Advertise();
        try
        {
            DataTable dt = new DataTable(); // chu y cho nay nek
            dt = DataProvider.GetDataTable("select * from Advertise where Adv_ID = " + Adv_ID);
            obj.Adv_ID = (dt.Rows[0]["Adv_ID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Adv_ID"];
            obj.Adv_Title = (dt.Rows[0]["Adv_Title"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Adv_Title"];
            obj.Adv_Link = (dt.Rows[0]["Adv_Link"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Adv_Link"];
            obj.Adv_Status = (dt.Rows[0]["Adv_Status"] is DBNull) ? false : (System.Boolean)dt.Rows[0]["Adv_Status"];
            obj.Adv_Position = (dt.Rows[0]["Adv_Position"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Adv_Position"];
            obj.Adv_Width = (dt.Rows[0]["Adv_Width"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Adv_Width"];
            obj.Adv_Height = (dt.Rows[0]["Adv_Height"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Adv_Height"];
            obj.Adv_CreateDate = (dt.Rows[0]["Adv_CreateDate"] is DBNull) ? DateTime.MinValue : (System.DateTime)dt.Rows[0]["Adv_CreateDate"];
            obj.Adv_EndDate = (dt.Rows[0]["Adv_EndDate"] is DBNull) ? DateTime.MinValue : (System.DateTime)dt.Rows[0]["Adv_EndDate"];
            obj.Adv_Desc = (dt.Rows[0]["Adv_Desc"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Adv_Desc"];
            obj.Adv_Image = (dt.Rows[0]["Adv_Image"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Adv_Image"];
            obj.Cate_ID = (dt.Rows[0]["Cate_ID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Cate_ID"];
            
        }
        catch (Exception e) { WebMsgBox.Show("ngoai le"); }
        return obj;
    }
    public static void Delete_Advertise(Advertise temp)
    {
        String strSQL;
        // xoa inv
        strSQL = "delete  from Advertise where Adv_ID = " + temp.Adv_ID;
        sqlConnection = new SqlConnection();
        sqlConnection = DataProvider.CreateConnection();
        SqlCommand cmd = new SqlCommand(strSQL, sqlConnection);
        cmd.CommandType = CommandType.Text; ;
        cmd.ExecuteNonQuery();
    }
    public static DataTable Get_AdvAsTable(String swhere, String sorder)
    {
        DataTable dt = new DataTable(); // chu y cho nay nek
        dt = DataProvider.GetDataTable("select * from Advertise " + swhere + sorder);
        return dt;
    }
    public static void Update_Advertise(Advertise temp)
    {

        sqlConnection = new SqlConnection();
        sqlConnection = DataProvider.CreateConnection();


        String strSQL;

        strSQL = "UPDATE Advertise  SET  Adv_Title = @Adv_Title , Adv_Link = @Adv_Link,Adv_Status = @Adv_Status, Adv_Position= @Adv_Position, Adv_Width = @Adv_Width, Adv_Height = @Adv_Height,Adv_CreateDate = @Adv_CreateDate , Adv_EndDate = @Adv_EndDate , Adv_Desc = @Adv_Desc, Adv_Image= @Adv_Image  WHERE Adv_ID= @Adv_ID ";
        SqlCommand sqlCommand = new SqlCommand(strSQL, sqlConnection);
        sqlCommand.CommandType = CommandType.Text;

        

        sqlCommand.CommandText = strSQL;
        sqlCommand.Parameters.Add("@Adv_ID", SqlDbType.Int).Value = temp.Adv_ID;
        sqlCommand.Parameters.Add("@Adv_Title", SqlDbType.NVarChar).Value = temp.Adv_Title;
        sqlCommand.Parameters.Add("@Adv_Link", SqlDbType.NVarChar).Value = temp.Adv_Link;
        sqlCommand.Parameters.Add("@Adv_Status", SqlDbType.Bit).Value = temp.Adv_Status;
        sqlCommand.Parameters.Add("@Adv_Position", SqlDbType.Int).Value = temp.Adv_Position;
        sqlCommand.Parameters.Add("@Adv_Width", SqlDbType.Int).Value = temp.Adv_Width;
        sqlCommand.Parameters.Add("@Adv_Height", SqlDbType.Int).Value = temp.Adv_Height;
        sqlCommand.Parameters.Add("@Adv_CreateDate", SqlDbType.DateTime).Value = temp.Adv_CreateDate;
        sqlCommand.Parameters.Add("@Adv_EndDate", SqlDbType.DateTime).Value = temp.Adv_EndDate;
        sqlCommand.Parameters.Add("@Adv_Desc", SqlDbType.NVarChar).Value = temp.Adv_Desc;
        sqlCommand.Parameters.Add("@Adv_Image", SqlDbType.NVarChar).Value = temp.Adv_Image;
//        sqlCommand.Parameters.Add("@Cate_ID", SqlDbType.Int).Value = temp.Cate_ID;

        sqlCommand.ExecuteNonQuery();// tai sao bi bao loi
        sqlCommand.Dispose();
        sqlCommand = null;

    }

}
